package com.newzqxq.koalamoney.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.common.BaseFragment;
import com.newzqxq.koalamoney.entity.HomeBean;
import com.newzqxq.koalamoney.ui.activity.ProductDetailsActivity;
import com.newzqxq.koalamoney.ui.adapter.HomeAdapter;
import com.newzqxq.koalamoney.utils.StatusBarUtil;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * 首页
 */

public class HomeFragment extends BaseFragment {
    @BindView(R.id.home_banner)
    MZBannerView home_banner;
    @BindView(R.id.homeRecyclerView)
    RecyclerView recyclerView;


    private List<HomeBean> data = new ArrayList<>();

    private HomeAdapter adapter;
    public static final String[] IMGS = new String[]{"http://img3.imgtn.bdimg.com/it/u=1157091805,2188328024&fm=26&gp=0.jpg",
            "http://img3.imgtn.bdimg.com/it/u=1704332224,615354977&fm=200&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=1996140688,1469391704&fm=200&gp=0.jpg"};


    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {

        return inflater.inflate(R.layout.fragment_home, null);
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarColor(getActivity(), R.color.colorPrimary);
        for (int i = 1; i < 4; i++) {
            HomeBean bean = new HomeBean("5" + i, "3" + i, i + "00", i);
            data.add(bean);
        }
        adapter = new HomeAdapter(data, getContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
        adapter.setmItemClickListener(new HomeAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view) {

                startActivity(new Intent(getActivity(), ProductDetailsActivity.class));
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        final List<String> list = new ArrayList<>();
        for (int i = 0; i < IMGS.length; i++) {
            list.add(IMGS[i]);
        }
        home_banner.setPages(list, new MZHolderCreator() {
            @Override
            public MZViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
    }

    /**
     * banner 显示图片
     */
    private class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);

            return view;
        }

        @Override
        public void onBind(Context context, final int i, final String s) {
            Glide.with(getActivity())
                    .load(s)
                    .bitmapTransform(new RoundedCornersTransformation(context, 15, 0, RoundedCornersTransformation.CornerType.ALL))
                    .error(R.drawable.img_sy_ban1)
                    .placeholder(R.drawable.img_sy_ban1)
                    .into(mImageView);

            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), i + "", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //banner 开始播放
        home_banner.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        //banner 停止播放
        home_banner.pause();
    }
}
